package com.ang.hutool.excel.web;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.ang.hutool.excel.simpleDemos.WriterList;
import com.ang.hutool.excel.util.DataUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ssang
 * @date 2021-09-04 14:45
 */
@Controller
public class DownloadExcelController {
    /**
     * 写出到IO
     */
    @RequestMapping("/downloadExcelIO")
    public void downloadExcelIO(){
        ExcelWriter writer = null;
        FileOutputStream out = null;
        try {
            // 通过工具类创建writer，默认创建xls格式
            writer = ExcelUtil.getWriter();

            //创建xlsx格式的
            //writer = ExcelUtil.getWriter(true);

            //构建数据
            WriterList listWriter = new WriterList();
            List<List<String>> rows = listWriter.getRows();

            // 一次性写出内容，使用默认样式，强制输出标题
            writer.write(rows, true);

            //out为OutputStream，需要写出到的目标流
            out = new FileOutputStream("d:/test1.xlsx");
            writer.flush(out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // 关闭writer，释放内存
            if(writer!=null){
                writer.close();
            }
        }
    }

    /**
     * 写出到servlet，客户端下载，xls
     */
    @RequestMapping("/downloadXls")
    public void downloadXls(HttpServletResponse response) throws IOException {
        // 通过工具类创建writer，默认创建xls格式
        ExcelWriter writer = ExcelUtil.getWriter();

        //构建数据
        WriterList listWriter = new WriterList();
        List<List<String>> rows = listWriter.getRows();

        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);

        //response为HttpServletResponse对象
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
        response.setHeader("Content-Disposition","attachment;filename=test.xls");

        ServletOutputStream out=response.getOutputStream();

        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }

    /**
     * 写出到servlet，客户端下载，xlsx
     */
    @RequestMapping("/downloadXlsx")
    public void downloadXlsx(HttpServletResponse response) throws IOException {
        com.ang.hutool.excel.util.ExcelUtil.downloadXlsx(response);
    }

    /**
     * 写出到servlet，客户端下载，xlsx
     */
    @RequestMapping("/downloadXlsx1")
    public void downloadXlsx1(HttpServletResponse response) throws Exception {
        List<DataUtil.UserBean> rows = DataUtil.getDataList();
        com.ang.hutool.excel.util.ExcelUtil.downloadXlsx1(response,"表格名","文件名",rows);
    }

    /**
     * 写出到servlet，客户端下载，xlsx
     */
    @RequestMapping("/downloadXlsx2")
    public void downloadXlsx2(HttpServletResponse response) throws Exception {
        List<DataUtil.UserBean> rows = DataUtil.getDataList();
        com.ang.hutool.excel.util.ExcelUtil.downloadXlsx2(response,"文件名",rows);
    }

    /**
     * 写出到servlet，客户端下载，xlsx
     */
    @RequestMapping("/downloadXlsx3")
    public void downloadXlsx3(HttpServletResponse response) throws Exception {
        List<DataUtil.UserBean> rows = DataUtil.getDataList();
        //定义要导出的属性和标题
        Map<String,String> aliasMap = new HashMap<>();
        aliasMap.put("name","姓名");
        aliasMap.put("age","年龄");
        com.ang.hutool.excel.util.ExcelUtil.downloadXlsx3(response,rows,aliasMap);
    }

    /**
     * 写出到servlet，客户端下载，xlsx
     */
    @RequestMapping("/downloadXlsx4")
    public void downloadXlsx4(HttpServletResponse response) throws Exception {
        //数据集合
        List<DataUtil.UserBean> rows = DataUtil.getDataList();
        //定义要导出的属性和标题
        Map<String,String> aliasMap = new HashMap<>();
        aliasMap.put("name","姓名");
        aliasMap.put("age","年龄");
        com.ang.hutool.excel.util.ExcelUtil.downloadXlsx4(response,rows,aliasMap);
    }
}
